package com.sbsj.data;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.sbsj.constants.CallStatusEnum;
import com.sbsj.constants.ErrorCodeEnum;
import com.sbsj.utils.JwtUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName ExchangeData
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author RodulphLiu
 * @Date 2017年3月31日 下午4:53:42
 * @version 1.0.0
 */
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ExchangeData<T> {

      private static final Logger logger = LoggerFactory.getLogger(ExchangeData.class);

      // 需要返回id情况下使用
      private String id;

      // 提示信息,前端/调用方直接显示
      private String message = "";

      private ErrorCodeEnum errorCode;

      @SuppressWarnings("unchecked")
      private T data = (T) new String[] {};
     
      private CallStatusEnum callStatus = CallStatusEnum.SUCCESS;
      
      public void markSuccess(String message){
        callStatus =CallStatusEnum.SUCCESS;
        this.message=message;
      }

      public void markWarning(String message)
      {
          callStatus=CallStatusEnum.WARNING;
          this.message=message;
      }

      public void markFail() {
        callStatus = CallStatusEnum.FAIL;
        this.message = "系统异常,请稍后再试或联系管理员";
      }
      
      public void markException(Exception e) {
        callStatus = CallStatusEnum.FAIL;
        this.message = e.getMessage();
         logger.error(e.getMessage(), e, ExchangeData.class);
      }
      
      public void markException(String message, Exception e) {
        callStatus = CallStatusEnum.FAIL;
        this.message = message;
         logger.error(e.getMessage(), e, ExchangeData.class);
      }
      
      public void markFail(String message) {
        callStatus = CallStatusEnum.FAIL;
        if (StringUtils.isNotBlank(message)) {
          this.message = message;
        } else {
          markFail();
        }
      }

}
